//跳转提交订单
$("#send").click(function(){
      //收货信息
      var addr=$("input[name='old_addr']:checked").val();
	  var province=$("[name='province']").val();
	  var city=$("[name='city']").val();
	  var zone=$("[name='zone']").val();
	  var street=$("[name='street']").val();
      var ship_name=$("input[name='ship_name']").val();
	  var ship_mobile=$("input[name='ship_mobile']").val();	
	  var cus_bb=$("[name='cus_bb']").val();	
	  var dlytime=$("[name='dlytime']").val();
	  //配送方式
	  var shipping=$("input[name='shipping']:checked").val();
	  //支付方式
	  var pay=$("input[name='pay']:checked").val();
	  var bank=$("input[name='bank']:checked").val();
	  //金额
	  var good_price=$("#good_price").text();
	  var dly_price=$("#dly_price").text();
	  var pmt_price=$("#pmt_price").text();
	  var total_price=$("#total_price").text();
	  var lp_price=$("[name=wallet]:checked").val();
	  //发票信息
	  var invoice=$("input[name='invoice']").val();
	  //优惠信息
	  var cou_id=$("[name=coupons]:checked").val();

	  if(addr=='no'&&(street==''||ship_name==''||ship_mobile=='')){
	       jNotify("<strong>请完善收货信息！</strong>",{VerticalPosition : 'center',HorizontalPosition : 'center'});
	  }else if(shipping==undefined){
	      jNotify("<strong>请选择配送方式！</strong>",{VerticalPosition : 'center',HorizontalPosition : 'center'});
	  }else if(pay==undefined){
	     jNotify("<strong>请选择支付方式！</strong>",{VerticalPosition : 'center',HorizontalPosition : 'center'});
	  }else{
		$("#sending").css("display","block");           
	    $.ajax({
		     type:"POST",
			 url:GROUP+"/Order/create_order",
			 dataType:"json",
			 data:{dlytime:dlytime,old_addr:addr,province:province,city:city,zone:zone,street:street,ship_mobile:ship_mobile,ship_name:ship_name,shipping:shipping,pay:pay,bank:bank,good_price:good_price,dly_price:dly_price,pmt_price:pmt_price,total_price:total_price,cus_msg:cus_bb,invoice:invoice,cou_id:cou_id,lp_price:lp_price},
			 success:function(data){
			     if(data.result=="success"){
					    jSuccess("<strong>订单提交成功！</strong>",{TimeShown : 3000,VerticalPosition : 'center',HorizontalPosition : 'center',onClosed:function(){ 
					    window.location.href=GROUP+"/Pay/ready/order/"+data.order_id;}})
				 }else{
				     jError("<strong>提交失败，请重试！</strong>",{VerticalPosition : 'center',HorizontalPosition : 'center'});
				}
			 }
		 })//ajax end
     }//if end
})
//初始化合计价格
$(document).ready(function(){
     var good_price=parseFloat($("#good_price").text());
	 var dly_price=parseFloat($("#dly_price").text());
	 var pmt_price=parseFloat($("#pmt_price").text());
	 var total_price=good_price+dly_price-pmt_price;
	 if(good_price<=0){
	 jNotify("<strong>先去挑选商品吧！</strong>",{TimeShown : 2000,VerticalPosition : 'center',HorizontalPosition : 'center',onClosed:function(){window.location.href="http://www.mayijie.com";}})
	 }
	 $("#total_price").html(total_price.toFixed(2));
})
//请求新地址
$("#new_addr").click(function(){
       $.ajax({
				    type:"GET",
					url:GROUP+"/Member/add_addr",
					dataType:"html",
					//data:{id:"9"},
					success:function(data){
					      $("#newaddr").html(data);
					 }
	   })
   
})
//选择已经存在地址
function changeAddr(t) {
     t.lastChild.checked='checked';
     for (var i = 0; i<t.parentNode.childNodes.length;i++) {
            if (t.parentNode.childNodes[i].className == 'cattsel') {
                   t.parentNode.childNodes[i].className = '';
            }
      }
          t.className = "cattsel";
		  countShip();
		  //countPay();
          $("#newaddr").empty();
}
//选择配送方式
function changeShip(t) {
    var addr=$("select[name='province']").val();
	var addr2=$("input[name='old_addr']:checked").val();
	var m_addr=$("#m_addr").val();
	if(addr2==undefined){
         jNotify("<strong>请先确认收货信息！</strong>",{VerticalPosition : 'center',HorizontalPosition : 'center'});
	}else{
			t.lastChild.checked='checked';
			for (var i = 0; i<t.parentNode.childNodes.length;i++) {
					if (t.parentNode.childNodes[i].className == 'cattsel') {
						t.parentNode.childNodes[i].className = '';
					}
			}
			t.className = "cattsel";
			countShip();
	}
}
//计算配送费用
function countShip(){
     var p1=$("select[name='province']").val();
	 var p2=$("input[name='old_addr']:checked").val();
	 var total_weight=parseFloat($("#total_weight").val());
	 var ship=$("input[name='shipping']:checked").val();

	 $.ajax({
				    type:"POST",
					url:URL+"/get_ship_money",
					dataType:"json",
					data:{ship:ship,province:p1,addr:p2},
					success:function(data){

					   if(total_weight<=data.one_w){
					      $("#dly_price").html(data.onefee);
					   }else{
					      var one_w=parseFloat(data.one_w);
						  var onefee=parseFloat(data.onefee);
						  var two_w=parseFloat(data.two_w);
						  var twofee=parseFloat(data.twofee);
						  var xu=Math.ceil((total_weight-one_w)/two_w);
						  var total=parseFloat(onefee+xu*twofee);
						  if(isNaN(total)){
						     $("#dly_price").html("请选择配送方式");
						  }else{
						     $("#dly_price").html(total.toFixed(2));
						  }
						  
					  }
					  countPay();
			      }
	   })//ajax结束	   
}



//合计需支付总金额
function countPay(){
   var good_price=parseFloat($("#good_price").text());
   var ship_price=parseFloat($("#dly_price").text());
   var pmt_price=parseFloat($("#pmt").val());
   var lp_price=parseFloat($("#lp_price").text());
   var pay=parseFloat(good_price+ship_price-pmt_price-lp_price);
   
   if(!isNaN(pay)){
	   if(pay<0){
		   $("#total_price").html('0.00');
	   }else{
		   $("#total_price").html(pay.toFixed(2));
	   }
   }
}
//获取支付银行
function getpay(t) {
     t.lastChild.checked='checked';
     for (var i = 0; i<t.parentNode.childNodes.length;i++) {
            if (t.parentNode.childNodes[i].className == 'cattsel') {
                   t.parentNode.childNodes[i].className = '';
            }
      }
	  t.className = "cattsel";
	  var pay=$("input[name='pay']:checked").val();
	  /*
	  $.ajax({
	     type:"GET",
		 url:GROUP+"/Pay/bank_choose",
		 dataType:"html",
		 data:{pay:pay},
		 success:function(data){
		     $("#payment").html(data);
		 }
	 })
	 */
}
$('a,input[type="button"],input[type="submit"]').bind('focus',function(){               
	     if(this.blur){                      
		 this.blur();                
		 };        
    });